// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConfigurationFieldAttribute.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.LiveObjects
{
    #region Usings

    using System;

    #endregion

    /// <summary>
    /// The configuration field attribute.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    [Serializable]
    public class ConfigurationFieldAttribute : Attribute
    {
        #region Constants and Fields

        /// <summary>
        /// The default schema.
        /// </summary>
        public const string DefaultSchema = "ConfigurationSchema";

        /// <summary>
        /// The default value.
        /// </summary>
        private object defaultValue;

        /// <summary>
        /// The generics type name.
        /// </summary>
        private string enumTypeName;

        /// <summary>
        /// Gets Name.
        /// </summary>
        private string name;

        /// <summary>
        /// Gets SchemaName.
        /// </summary>
        private string schemaName;

        /// <summary>
        /// Gets ToolTip.
        /// </summary>
        private string toolTip;

        /// <summary>
        /// Gets Type.
        /// </summary>
        private Type type;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationFieldAttribute"/> class.
        /// Usable only for "enum" configuration fields.
        /// </summary>
        /// <param name="configurationName">
        /// The configuration name.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="enumType">
        /// ConfigurationField.Enum only.
        /// </param>
        /// <param name="enumRealType">
        /// The enum real type (ex: typeof(ETradingScope))
        /// </param>
        /// <param name="tip">
        /// The tip.
        /// </param>
        public ConfigurationFieldAttribute(
            string configurationName, string name, Type enumType, Type enumRealType, string tip)
        {
            this.Init(configurationName, name, enumType, enumRealType, tip);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationFieldAttribute"/> class.
        /// A configuration field for a specified configuration name.
        /// </summary>
        /// <param name="configurationName">
        /// The configuration name.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="type">
        /// The type taken from one of these :
        /// 
        /// ConfigurationField.File
        /// ConfigurationField.Table
        /// ConfigurationField.Decimal
        /// ConfigurationField.Int
        /// ConfigurationField.Bool
        /// ConfigurationField.String
        /// </param>
        /// <param name="tip">
        /// The tip.
        /// </param>
        public ConfigurationFieldAttribute(string configurationName, string name, Type type, string tip)
        {
            this.Init(configurationName, name, type, null, tip);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationFieldAttribute"/> class.
        /// Usable only for "enum" configuration fields.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="enumType">
        /// ConfigurationField.Enum only.
        /// </param>
        /// <param name="enumRealType">
        /// The enum real type (ex: typeof(ETradingScope))
        /// </param>
        /// <param name="tip">
        /// The tip.
        /// </param>
        public ConfigurationFieldAttribute(string name, Type enumType, Type enumRealType, string tip)
        {
            this.Init(DefaultSchema, name, enumType, enumRealType, tip);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationFieldAttribute"/> class.
        /// An configuration field for the default configuration name.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="type">
        /// The type taken from one of these :
        /// 
        /// ConfigurationField.File
        /// ConfigurationField.Table
        /// ConfigurationField.Decimal
        /// ConfigurationField.Int
        /// ConfigurationField.Bool
        /// ConfigurationField.String
        /// </param>
        /// <param name="tip">
        /// The tip.
        /// </param>
        public ConfigurationFieldAttribute(string name, Type type, string tip)
        {
            this.Init(DefaultSchema, name, type, null, tip);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets DefaultValue.
        /// </summary>
        /// <exception cref="Exception">
        /// </exception>
        public object DefaultValue
        {
            get
            {
                return this.defaultValue;
            }

            set
            {
                // Only store "as is" primitive types and strings. Else store string representation
                if (value.GetType().IsPrimitive || value.GetType().Equals(typeof(string)))
                {
                    this.defaultValue = value;
                }
                else
                {
                    this.defaultValue = value.ToString();
                }
            }
        }

        /// <summary>
        /// Gets EnumTypeName, is not null only if we are in presence of an "enum" attribute.
        /// </summary>
        public string EnumTypeName
        {
            get
            {
                return this.enumTypeName;
            }
        }

        /// <summary>
        /// Gets whether mandatory.
        /// </summary>
        public bool IsMandatory { get; set; }

        /// <summary>
        /// Gets Name.
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }
        }

        /// <summary>
        /// Gets SchemaName.
        /// </summary>
        public string SchemaName
        {
            get
            {
                return this.schemaName;
            }
        }

        /// <summary>
        /// Gets ToolTip.
        /// </summary>
        public string ToolTip
        {
            get
            {
                return this.toolTip;
            }
        }

        /// <summary>
        /// Gets Type.
        /// </summary>
        public Type Type
        {
            get
            {
                return this.type;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The is supported mandatory type.
        /// </summary>
        /// <param name="t">
        /// The t.
        /// </param>
        /// <returns>
        /// The is supported mandatory type.
        /// </returns>
        public static bool IsSupportedMandatoryType(Type t)
        {
            if (Reflection.ImplementsInterface(t, typeof(ConfigurationField).FullName))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// The is supported optional type.
        /// </summary>
        /// <param name="t">
        /// The t.
        /// </param>
        /// <returns>
        /// The is supported optional type.
        /// </returns>
        public static bool IsSupportedOptionalType(Type t)
        {
            if (Reflection.ImplementsInterface(t, typeof(ConfigurationField).FullName))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The to string.
        /// </returns>
        public override string ToString()
        {
            return string.Format("Name={0} DefaultValue={1}", this.Name, this.DefaultValue);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The Init.
        /// </summary>
        /// <param name="configurationName">
        /// The configuration name.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="type">
        /// The type.
        /// </param>
        /// <param name="genericsType">
        /// The generics type.
        /// </param>
        /// <param name="tip">
        /// The tip.
        /// </param>
        private void Init(string configurationName, string name, Type type, Type genericsType, string tip)
        {
            this.schemaName = configurationName;
            this.name = name;
            this.type = type;
            this.toolTip = tip;
            if (genericsType != null)
            {
                this.enumTypeName = genericsType.FullName;
            }
        }

        #endregion
    }
}